package com.yxx.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import com.yxx.common.annotation.RepeatSubmit;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.annotation.Log;
import com.yxx.common.core.controller.BaseControllerPlus;
import com.yxx.common.enums.BusinessType;
import com.yxx.business.entity.TbOperationPhaseDo;
import com.yxx.business.entity.TbOperationPhaseVo;
import com.yxx.business.entity.TbOperationPhaseQueryDto;
import com.yxx.business.entity.TbOperationPhaseEditDto;
import com.yxx.business.service.ITbOperationPhaseService;
import com.yxx.common.utils.poi.ExcelUtil;
import com.yxx.common.core.domain.R;
import com.yxx.common.core.domain.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;

import org.springframework.validation.annotation.Validated;


/**
 * 运营维护阶段Controller
 *
 * @author yxx
 * @date 2025-08-16
 */
@Tag(name = "运营维护阶段管理-TbOperationPhase")
@RestController
@RequestMapping("/business/operationPhase")
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbOperationPhaseController extends BaseControllerPlus {

    private final ITbOperationPhaseService tbOperationPhaseService;

    @Operation(summary = "查询--运营维护阶段列表")
    @GetMapping("/list")
    public PageResult<TbOperationPhaseVo> list(@ParameterObject TbOperationPhaseQueryDto tbOperationPhaseQueryDto) {
        return tbOperationPhaseService.selectTbOperationPhaseVoPage(tbOperationPhaseQueryDto);
    }

    @Operation(summary = "查询--运营维护阶段单个")
    @GetMapping(value = "/{operationId}")
    public R<TbOperationPhaseVo> getInfo(@PathVariable Long operationId) {
        return R.ok(tbOperationPhaseService.selectTbOperationPhaseVoByOperationId(operationId));
    }

    @Operation(summary = "新增--运营维护阶段")
    @Log(title = "运营维护阶段", businessType = BusinessType.INSERT)
    @PostMapping
    public R<String> add(@RequestBody @Validated TbOperationPhaseEditDto tbOperationPhaseQueryDto) {
        return toResult(tbOperationPhaseService.insertTbOperationPhase(tbOperationPhaseQueryDto));
    }

    @Operation(summary = "修改--运营维护阶段")
    @Log(title = "运营维护阶段", businessType = BusinessType.UPDATE)
    @PutMapping("/{operationId}")
    @RepeatSubmit(interval = 2000, message = "您操作的太频繁了，结果正在保存中，请稍后重试")
    public R<Integer> update(@PathVariable Long operationId, @RequestBody @Validated TbOperationPhaseEditDto tbOperationPhaseQueryDto) {
        return R.ok(tbOperationPhaseService.updateTbOperationPhase(operationId, tbOperationPhaseQueryDto));
    }

    @Operation(summary = "删除--运营维护阶段")
    @Log(title = "运营维护阶段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{operationIds}")
    public R<String> delete(@PathVariable List<Long> operationIds) {
        return toResult(tbOperationPhaseService.deleteTbOperationPhaseByOperationIds(operationIds));
    }

    @Operation(summary = "导出--运营维护阶段列表")
    @PreAuthorize("@ss.hasPermi('business:operationPhase:export')")
    @Log(title = "运营维护阶段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ParameterObject TbOperationPhaseQueryDto tbOperationPhaseQueryDto) {
        List<TbOperationPhaseDo> list = tbOperationPhaseService.selectTbOperationPhaseDoList(tbOperationPhaseQueryDto);
        ExcelUtil<TbOperationPhaseDo> util = new ExcelUtil<>(TbOperationPhaseDo.class);
        util.exportExcel(response, list, "运营维护阶段数据");
    }

    @Operation(summary = "导出--运营维护阶段模板")
    @PreAuthorize("@ss.hasPermi('business:operationPhase:import')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        ExcelUtil<TbOperationPhaseDo> util = new ExcelUtil<>(TbOperationPhaseDo.class);
        util.importTemplateExcel(response, "导入运营维护阶段模板数据");
    }

    @Operation(summary = "导入--运营维护阶段列表")
    @PreAuthorize("@ss.hasPermi('business:operationPhase:import')")
    @Log(title = "运营维护阶段", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<Boolean> importData(MultipartFile file) throws IOException {
        ExcelUtil<TbOperationPhaseDo> util = new ExcelUtil<>(TbOperationPhaseDo.class);
        List<TbOperationPhaseDo> list = util.importExcel(file.getInputStream());
        return R.ok(tbOperationPhaseService.saveBatch(list), "成功导入 " + list.size() + " 条记录");
    }
}
